/*
 * @Description: 规范性检测统计
 * @Author: Zzt
 * @Date: 2022-11-11 14:52:39
 * @LastEditTime: 2023-01-18 10:09:52
 */
import VisualizationMain from '@/components/DataVisualization/VisualizationMain';
import Nav from '@/components/Nav';
import { Tabs } from 'antd';
import { CheckboxValueType } from 'antd/es/checkbox/Group';
import classNames from 'classnames/bind';
import { useEffect, useState } from 'react';
import styles from '../styles/normativeCensus.module.scss';
import { useNormativeCensusContext } from '../contexts/NormativeCensusContext';
const { TabPane } = Tabs;
const cx = classNames.bind(styles);

const according = [
  { fondName: '按年度统计', id: 0 },
  { fondName: '按操作人', id: 1 }
];
const segmentedList = [
  { label: '检测次数', value: 'times' },
  { label: '检测卷数', value: 'folderCounts' },
  { label: '检测文件数', value: 'fileCounts' },
  { label: '检测电子文件数', value: 'efileCounts' }
];
const table: any = [{ title: '年度', dataIndex: 'yearName', key: 'yearName' }];
const NormativeCensusWarp = () => {
  const { setChartData, setTableData, dataList, chartList, resultFilter, detectionTypeFilter } =
    useNormativeCensusContext();
  const [limit, setLimit] = useState<any>({});
  const [segmented, setSegmented] = useState('foldertimesCounts');
  const [fondValue, setFondValue] = useState({});
  // 选中的按照类型
  const [type, setType] = useState(0);
  const [options, setOptions] = useState([]);
  const [visibleData, setVisibleData] = useState([]);
  const [condition, setCondition] = useState(<></>);
  const [tableList, setTableList] = useState(table);
  const [xData, setXData] = useState([]);
  const [yData, setYData] = useState([]);
  const [listQuery, setListQuery] = useState({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });
  /**
   * @description: 切换全宗
   * @param {number} val 选中的全宗
   */
  const handleAccordingChange = (val: any) => {
    console.log(val);
    setType(parseInt(val, 10));
  };
  const onChange = (checkedValues: CheckboxValueType[]) => {
    console.log('checked = ', checkedValues);
  };

  useEffect(() => {
    setChartData({ ...limit, type, ...fondValue });
    setListQuery((state) => {
      return { ...state, pageNo: 1 };
    });
  }, [limit, type, fondValue]);

  useEffect(() => {
    const param = { ...limit, type, ...fondValue };
    setTableData({ ...listQuery, param });
  }, [limit, type, fondValue, listQuery]);

  useEffect(() => {
    setVisibleData(dataList);
    setYData(chartList.map((item: any) => item.name));
    setXData(chartList.map((item: any) => item[segmented]));
  }, [dataList, chartList, segmented]);

  const onChangeNav = (value: any) => {
    setFondValue(value);
  };

  const ChangNo = (num: any) => {
    console.log('num', num);
    setListQuery((state) => {
      return { ...state, pageNo: num };
    });
  };
  useEffect(() => {
    console.log('table', table);
    const removed = table.slice(0);
    if (type === 0) {
      removed.push(
        { title: '全宗', dataIndex: 'fondsName', key: 'fondsName' },
        { title: '档案类型', dataIndex: 'archiveTypeName', key: 'archiveTypeName' },
        { title: '业务阶段', dataIndex: 'businessStage', key: 'businessStage' },
        {
          title: '检测类型',
          dataIndex: 'detectionType',
          key: 'detectionType',
          render: (type: number) => detectionTypeFilter(type)
        },
        {
          title: '检测状态',
          dataIndex: 'detectionResult',
          render: (type: number) => resultFilter(type),
          key: 'detectionResult'
        },
        { title: '检查次数', dataIndex: 'times', key: 'times' },
        { title: '检测卷数', dataIndex: 'folderCounts', key: 'folderCounts' },
        { title: '检测文件数', dataIndex: 'fileCounts', key: 'fileCounts' },
        { title: '检测电子文件数', dataIndex: 'eFileCounts', key: 'eFileCounts' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '业务阶段',
          name: 'businessStage',
          checkList: [
            { value: 1, label: '接收检查' },
            { value: 2, label: '移交检查' },
            { value: 3, label: '管理检查' },
            { value: 4, label: '长期保存检查' }
          ]
        },
        {
          title: '检测类型',
          name: 'detectionType',
          checkList: [
            { value: 1, label: '条目与原文检测' },
            { value: 2, label: '条目检测' },
            { value: 3, label: '原文检测' },
            { value: 4, label: 'EEP检测' },
            { value: 5, label: '光盘检测' }
          ]
        },
        {
          title: '检测状态',
          name: 'detectionResult',
          checkList: [
            { value: 1, label: '检测成功' },
            { value: 2, label: '检测失败' }
          ]
        }
      ]);
      setCondition(<Nav onChangeNav={onChangeNav} />);
    }
    if (type === 1) {
      removed.push(
        { title: '操作人', dataIndex: 'operatorName', key: 'operatorName' },
        { title: '检查次数', dataIndex: 'times', key: 'times' },
        { title: '检查成功数', dataIndex: 'succTotal', key: 'succTotal' },
        { title: '检查失败数', dataIndex: 'failTotal', key: 'failTotal' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '操作人',
          name: 'operator',
          checkList: [
            { value: 1, label: '操作人1' },
            { value: 2, label: '操作人2' },
            { value: 3, label: '操作人3' },
            { value: 4, label: '操作人4' },
            { value: 5, label: '操作人5' }
          ]
        }
      ]);
      setCondition(<></>);
    }
  }, [type]);
  /* 改变触发 */
  const onChangeYear = (value: any) => {
    setLimit(value.chioce);
    setSegmented(value.segmentValue);
  };
  return (
    <>
      <Tabs tabPosition="left" className={cx('tab')} onTabClick={handleAccordingChange}>
        {according.map((item) => (
          <TabPane tab={<>{item.fondName}</>} key={item.id}>
            <VisualizationMain
              getList={setTableData}
              type={item.fondName}
              condition={condition}
              segmented={segmentedList}
              tableList={tableList}
              choiceList={options}
              data={{ visibleData, xData, yData }}
              onChange={onChangeYear}
              ChangNo={ChangNo}
            />
          </TabPane>
        ))}
      </Tabs>
    </>
  );
};
export default NormativeCensusWarp;
